<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link th:href="@{/ajax/libs/summernote/summernote.css}" rel="stylesheet"/>
<link th:href="@{/ajax/libs/summernote/summernote-bs3.css}" rel="stylesheet"/>
<body class="white-bg">

<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-jiaotong-edit" th:object="${jiaotong}">
        <input id="jtId" name="jtId" th:field="*{jtId}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">标题：</label>
            <div class="col-sm-8">
                <input id="jtTitle" name="jtTitle" th:field="*{jtTitle}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">变更日期：</label>
            <div class="col-sm-8">
                <input id="jtNewChangedate" name="jtNewChangedate" th:field="*{jtNewChangedate}" class="time-input"
                       type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">信息来源：</label>
            <div class="col-sm-8">
                <input id="jtOriginal" name="jtOriginal" th:field="*{jtOriginal}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">一级类别：</label>
            <div class="col-sm-8">
                <select id="category1" class="form-control m-b" name="jtLb1id"
                        data-rel="chosen">
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">二级类别：</label>
            <div class="col-sm-8">
                <select id="category2" class="form-control m-b" name="jtLb2id"
                        data-rel="chosen">
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">三级类别：</label>
            <div class="col-sm-8">
                <select id="category3" class="form-control m-b" name="jtLb3id"
                        data-rel="chosen">
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">所在省：</label>
            <div class="col-sm-8">
                <select id="area1" class="form-control m-b" name="jtProvinceid"
                        data-rel="chosen">
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">所在市：</label>
            <div class="col-sm-8">
                <select id="area2" class="form-control m-b" name="jtCityid"
                        data-rel="chosen">
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">所在县：</label>
            <div class="col-sm-8">
                <select id="area3" class="form-control m-b" name="jtCountryid"
                        data-rel="chosen">
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">总名称：</label>
            <div class="col-sm-8">
                <input id="jtZongmc" name="jtZongmc" th:field="*{jtZongmc}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">总编号：</label>
            <div class="col-sm-8">
                <input id="jtZongbh" name="jtZongbh" th:field="*{jtZongbh}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分段名称：</label>
            <div class="col-sm-8">
                <input id="jtFendmc" name="jtFendmc" th:field="*{jtFendmc}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分段编号：</label>
            <div class="col-sm-8">
                <input id="jtFendbh" name="jtFendbh" th:field="*{jtFendbh}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">级别：</label>
            <div class="col-sm-8">
                <input id="jtLevels" name="jtLevels" th:field="*{jtLevels}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">里程：</label>
            <div class="col-sm-8">
                <input id="jtLicheng" name="jtLicheng" th:field="*{jtLicheng}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">详细内容：</label>
            <div class="col-sm-8">
                <input id="jtDetails" name="jtDetails" th:field="*{jtDetails}" type="hidden">
                <div id="editor" class="summernote"></div>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/summernote/summernote.min.js}"></script>
<script th:src="@{/ajax/libs/summernote/summernote-zh-CN.js}"></script>
<script type="text/javascript" th:inline="javascript">
    var prefix = ctx + "system/jiaotong";
    var prefix1 = ctx + "system/category"
    var prefix2 = ctx + "system/area"
    $(function () {
        var lb1 = [[${jiaotong.jtLb1id}]];
        var lb2 = [[${jiaotong.jtLb2id}]];
        var lb3 = [[${jiaotong.jtLb3id}]];

        var szx1 = [[${jiaotong.jtProvinceid}]];
        var szx2 = [[${jiaotong.jtCityid}]];
        var szx3 = [[${jiaotong.jtCountryid}]];

        initCategrey(lb1, lb2, lb3);
        initArea(szx1, szx2, szx3);
        $('.summernote').summernote({
            height: '220px',
            lang: 'zh-CN',
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });
        var content = $("#jtDetails").val();
        $('#editor').summernote('code', content);
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    // $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    $(obj).summernote('editor.insertImage', img_root + result.fileName, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    $("#form-jiaotong-edit").validate({
        rules: {
            xxxx: {
                required: true,
            },
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#jtDetails").val(sHTML);
            $.operate.save(prefix + "/edit", $('#form-jiaotong-edit').serialize());
        }
    }

    function initCategrey(jtLb1id, jtLb2id, jtLb3id) {


        cate1(jtLb1id);
        cate2(jtLb1id, jtLb2id);
        cate3(jtLb2id, jtLb3id);
        $("#category1").change(function () {
            cate2($("#category1").val());

        });
        $("#category2").change(function () {
            cate3($("#category2").val());
        });

    }

    function cate1(id) {
        $.get(
            prefix1 + "/list?parentId=100&deptId=101",
            function (data) {
                if (data) {
                    var result = "";
                    $.each(data, function (n, value) {

                        if (value.deptId == id)
                            result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
                        else
                            result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";

                    });
                    $("#category1").html(result);
                }
            }, "json");
    }

    function cate2(id, deptId) {

        $.get(
            prefix1 + "/list?parentId=101",
            function (data) {
                if (data) {
                    var result = ''
                    $.each(data, function (n, value) {

                        if (value.deptId == deptId)
                            result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
                        else
                            result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";

                    });
                    $("#category2").html(result);

                }
            }, "json");
    }

    function cate3(id, deptId) {
        if (!id) id = -100;
        $.get(
            prefix1 + "/list?parentId=" + id,
            function (data) {
                if (data) {
                    var result = '';
                    $.each(data, function (n, value) {
                        if (value.deptId == deptId)
                            result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
                        else
                            result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
                    });

                    $("#category3").html(result);
                }
            }, "json");
    }

    function initArea(jtProvinceid, jtCityid, jtCountryid) {


        farea1(jtProvinceid);
        farea21(jtProvinceid, jtCityid);
        farea3(jtCityid, jtCountryid);
        $("#area1").change(function () {
            farea2($("#area1").val());

        });
        $("#area2").change(function () {
            farea3($("#area2").val());
        });

    }

    function farea1(id) {
        $.get(
            prefix2 + "/list?parentId=1",
            function (data) {
                if (data) {
                    var result = "";
                    $.each(data, function (n, value) {

                        if (value.deptId == id)
                            result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
                        else
                            result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";

                    });
                    $("#area1").html(result);
                }
            }, "json");
    }

    function farea2(id, deptId) {
        $.get(
            prefix2 + "/list?parentId=" + id,
            function (data) {
                if (data) {
                    var result = "";
                    $.each(data, function (n, value) {

                        if (value.deptId == deptId) {
                            result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
                        }
                        else {
                            result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
                            if (n == 0) {
                                farea3(value.deptId);
                            }
                        }
                    });
                    $("#area2").html(result);

                }
            }, "json");
    }

    function farea21(id, deptId) {
        $.get(
            prefix2 + "/list?parentId=" + id,
            function (data) {
                if (data) {
                    var result = "";
                    $.each(data, function (n, value) {

                        if (value.deptId == deptId) {
                            result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
                        }
                        else {
                            result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
                        }
                    });
                    $("#area2").html(result);

                }
            }, "json");
    }

    function farea3(id, deptId) {
        $.get(
            prefix2 + "/list?parentId=" + id,
            function (data) {
                if (data) {
                    var result = '';
                    $.each(data, function (n, value) {

                        if (value.deptId == deptId)
                            result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
                        else
                            result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
                    });

                    $("#area3").html(result);
                }
            }, "json");
    }
</script>
</body>
</html>
